package com.study.talk.app.home.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.study.talk.R;
import com.study.talk.adapter.ViewPager2StateAdapter;
import com.study.talk.app.base.BaseFragment;
import com.study.talk.databinding.FragmentContactsBinding;

import java.util.ArrayList;
import java.util.List;

public class ContactsFragment extends BaseFragment {


    private AppCompatActivity activity;
    private FragmentContactsBinding fragmentContactsBinding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (AppCompatActivity) getActivity();
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        fragmentContactsBinding = FragmentContactsBinding.inflate(inflater, container, false);
        initDate();
        return fragmentContactsBinding.getRoot();
    }

    private void initDate() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new FriendFragment());
        fragmentList.add(new GroupFragment());
        fragmentContactsBinding.contactsTab.addTab(fragmentContactsBinding.contactsTab.newTab());
        fragmentContactsBinding.contactsTab.addTab(fragmentContactsBinding.contactsTab.newTab());
        String[] titles = {"好友", "群聊"};
        ViewPager2StateAdapter adapter = new ViewPager2StateAdapter(activity, fragmentList);
        fragmentContactsBinding.contactsViewpage.setAdapter(adapter);
        TabLayoutMediator mediator = new TabLayoutMediator(fragmentContactsBinding.contactsTab, fragmentContactsBinding.contactsViewpage, (tab, position) -> tab.setText(titles[position]));
        mediator.attach();
    }
}